from typing import List


class Solution:
    def findMiddleIndex(self, nums: List[int]) -> int:
        size = len(nums)

        prefix = [0] * size
        suffix = [0] * size
        for i in range(1, size):
            prefix[i] = nums[i - 1] + prefix[i - 1]
        for i in range(size - 2, -1, -1):
            suffix[i] = nums[i + 1] + suffix[i + 1]

        for i in range(size):
            if prefix[i] == suffix[i]:
                return i
        return -1


if __name__ == "__main__":
    print(Solution().findMiddleIndex(nums=[2, 3, -1, 8, 4]))  # 3
    print(Solution().findMiddleIndex(nums=[1, -1, 4]))  # 2
    print(Solution().findMiddleIndex(nums=[2, 5]))  # -1
    print(Solution().findMiddleIndex(nums=[1]))  # 0
